package com.shierke.Ume.ui.activity.explore

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.shierke.Ume.R
import com.shierke.Ume.base.BaseNoTitleActivity
import com.shierke.Ume.ui.fragment.explore.ExploreUserFansFollowFragment
import com.shierke.Ume.ui.fragment.explore.ExploreUserFansFollowerFragment
import com.shierke.Ume.ui.view.HXLinePagerIndicator
import com.shierke.Ume.util.click
import com.shierke.Ume.util.startActivity
import kotlinx.android.synthetic.main.activity_explore_user_fans.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ClipPagerTitleView

/**
 * Create by Ybx
 * on 2020-10-16
 * Desribe:
 */
class ExploreUserFansListActivity : BaseNoTitleActivity() {


    companion object {
        fun startActivity(context: Context) {
            context.startActivity<ExploreUserFansListActivity>()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_explore_user_fans)


        titles.add(getString(R.string.explore_user_fans_title_follow))
        titles.add(getString(R.string.explore_user_fans_title_follower))

        fragments.add(ExploreUserFansFollowFragment())
        fragments.add(ExploreUserFansFollowerFragment())


        val viewpagerAdapter = ViewpagerAdapter(supportFragmentManager)
        fansViewPager.adapter =viewpagerAdapter

        var commonNavigator = CommonNavigator(this)
        //commonNavigator.isAdjustMode = true//设置title宽度自适应
        commonNavigator.isEnablePivotScroll = true

        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getTitleView(context: Context?, index: Int): IPagerTitleView {
                val clipPagerTitleView = ClipPagerTitleView(context)
                clipPagerTitleView.text = titles[index]
                clipPagerTitleView.textSize = UIUtil.dip2px(context, 20.0).toFloat()
                clipPagerTitleView.textColor = Color.parseColor("#9C9C9C")
                clipPagerTitleView.clipColor = Color.parseColor("#2C2C2C")


                clipPagerTitleView.click {

                    if (fansViewPager != null) {
                        fansViewPager.currentItem = index
                    }
                }
                return clipPagerTitleView
            }

            override fun getCount() = titles.size

            override fun getIndicator(context: Context?): IPagerIndicator? {

                val indicator = HXLinePagerIndicator(context)
                indicator.mode = LinePagerIndicator.MODE_EXACTLY
                indicator.lineHeight = UIUtil.dip2px(context, 6.0).toFloat()
                indicator.lineWidth = UIUtil.dip2px(context, 40.0).toFloat()
                indicator.roundRadius = UIUtil.dip2px(context, 3.0).toFloat()
                indicator.startInterpolator = AccelerateInterpolator()
                indicator.endInterpolator = DecelerateInterpolator(2.0f)
                return indicator
            }
        }

        magic_indicator.navigator = commonNavigator
        ViewPagerHelper.bind(magic_indicator, fansViewPager)


    }


    private var fragments = arrayListOf<Fragment>()
    private var titles = arrayListOf<String>()


    inner class ViewpagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

        override fun getCount() = fragments.size

        override fun getItem(position: Int) = fragments[position]

        override fun getPageTitle(position: Int): CharSequence? {
            return titles[position]
        }
    }


}